local zishucup__bugu = fk.CreateSkill{
  name = "zishucup__bugu",
}

Fk:loadTranslationTable{
  ["zishucup__bugu"] = "不孤",
  [":zishucup__bugu"] = "准备阶段，经你同意，其他角色可以与你交换手牌。你死亡时，经其他角色同意，你可以与其交换手牌。",

  ["#zishucup__bugu1-choose"] = "不孤：是否向%dest发起交换手牌的请求？若其同意，你与其交换手牌。",
  ["#zishucup__bugu1-comfirm"] = "不孤：%dest请求与你交换手牌，点确定同意，点取消拒绝。",
  ["#zishucup__bugu2-choose"] = "不孤：请选择一名其他角色，若其同意，你与其交换手牌。",
  ["#zishucup__bugu2-comfirm"] = "不孤：%dest请求与你交换手牌，点确定同意，点取消拒绝。",

  ["$zishucup__bugu1"] = " ",
  ["$zishucup__bugu2"] = " ",
}

zishucup__bugu:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(zishucup__bugu.name) and player.phase == Player.Start
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    for _, p in ipairs(player.room:getOtherPlayers(player)) do
      if not p.dead and not player.dead then
        local comfirm1 = player.room:askToSkillInvoke(p, { skill_name = zishucup__bugu.name, prompt = "#zishucup__bugu1-choose::"..player.id,})
        if comfirm1 then
          local comfirm2 = player.room:askToSkillInvoke(player, { skill_name = zishucup__bugu.name, prompt = "#zishucup__bugu1-comfirm::"..p.id,})
          if comfirm2 then
            player.room:swapAllCards(player, {player, p}, zishucup__bugu.name)
          end
        end
      end
    end
  end,
})

zishucup__bugu:addEffect(fk.Death, {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(zishucup__bugu.name, false, true)
  end,
  on_cost = function(self, event, target, player, data)
    local to = player.room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = player.room:getOtherPlayers(player),
      skill_name = zishucup__bugu.name,
      cancelable = true,
      prompt = "#zishucup__bugu2-choose",
    })
    if #to == 1 then
      event:setCostData(self, {tos = to[1]})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local to = event:getCostData(self).tos
    if player.room:askToSkillInvoke(to, {
      skill_name = zishucup__bugu.name,
      prompt = "#zishucup__bugu2-comfirm::"..player.id,
    }) then
      player.room:swapAllCards(player, {player, to}, zishucup__bugu.name)
    end
  end,
})

return zishucup__bugu
